package com.jcnet.bizaia;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.bean.SPSetting;
import com.jcnet.bizaia.bean.SPUserBean;
import com.jcnet.bizaia.bean.UserBean;
import com.jcnet.bizaia.bean.UserHttpBean;
import com.jcnet.bizaia.tools.BBTools;
import com.jcnet.bizaia.tools.DataStorageHelper;
import com.jcnet.bizaia.view.ProgressHUD;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 
 * @author chenguandong
 * 
 * 
 * private EditText usernameEditText;
	
	private EditText passwordEditText;
	setContentView(R.layout.login);
	
	usernameEditText =(EditText)findViewById(R.id.username);
	passwordEditText =(EditText)findViewById(R.id.password);
	
	usernameEditText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.user, 0, R.drawable.erroe,0 );
 *
 */
public class LoginActivity extends Activity {
	
	private EditText usernmaEditText , passwordEditText;
	
	private AsyncHttpClient asyncHttpClient;
	
	private RequestParams params;
	
	private ProgressHUD progressHUD;
	
	private String username;
	
	private String password;
	
	private Button loginButton;
	
	private Button registButton;
	
	private Gson gson;
	
	private DataStorageHelper dataStorageHelper;
	
	private int jumpMark;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		dataStorageHelper = new DataStorageHelper(this, SPUserBean.dataTableNameForUser);
		
		jumpMark = getIntent().getIntExtra("firstIn", -1);
		
		asyncHttpClient = new AsyncHttpClient();
		params = new RequestParams();
		
		usernmaEditText = (EditText)findViewById(R.id.username);
		passwordEditText = (EditText)findViewById(R.id.password);
		
		loginButton = (Button)findViewById(R.id.loginButton);
		registButton = (Button)findViewById(R.id.registerBut);
		
		
		gson = new Gson();
		registButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
				intent.putExtra("jumpMark", jumpMark);
				startActivity(intent);
				
			}
		});
		
		
		findViewById(R.id.forgetPassword).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//startActivity(new Intent(LoginActivity.this, FindBackPWDActivity.class));
				
			}
		});
		
		
			loginButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(usernmaEditText.getText()!=null&&passwordEditText.getText()!=null){
						
						username = usernmaEditText.getText().toString().trim();
						
						password = passwordEditText.getText().toString().trim();
						
						if(BBTools.isEmptyString(username)&&BBTools.isEmptyString(password))
						{
							
							password = BBTools.getMD5Str(password);
							
							
							params.put("user.usermail", username);
							params.put("user.userpwd", password);
							params.put("languageNum", App.localeCode);
							
							asyncHttpClient.get(App.HTTP_PATH+"use_showUserOne.action?", params, new AsyncHttpResponseHandler(){
								
								public void onStart() {
									
								progressHUD=	ProgressHUD.show(LoginActivity.this, getString(R.string.loging), true, true	, null);
								};
								
								public void onSuccess(String arg0) {
									
									System.out.println(arg0+"__________________");
									
									if(arg0!=null){
										//System.out.println(arg0);
										UserHttpBean stateBean = gson.fromJson(arg0, new TypeToken<UserHttpBean>(){}.getType());
										if(stateBean!=null&&stateBean.getState()==200){
											
											UserBean userBean  = stateBean.getObjJson();
											
											
											//存储保留数据到本地
											dataStorageHelper.putInt(SPUserBean.userIDSP_1, userBean.getUserid());
											dataStorageHelper.put(SPUserBean.regmailSP_6, userBean.getUsermail());
											dataStorageHelper.put(SPUserBean.userImgSP_4, userBean.getUserimage());
											dataStorageHelper.put(SPUserBean.nikeNameSP_2, userBean.getNickname());
											dataStorageHelper.putInt(SPUserBean.vipsumSP_5, userBean.getVipsum());
											
											
											/*DataStorageHelper settingDataStorageHelper = new DataStorageHelper(LoginActivity.this, SPSetting.SETTING_TABLE_NAME);
											if(settingDataStorageHelper.getBoolean(SPSetting.IS_FIRST_APP_IN)==false){
												settingDataStorageHelper.putBoolean(SPSetting.IS_FIRST_APP_IN, true);
												startActivity(new Intent(LoginActivity.this, FirstInAdressActivity.class));
											}*/
											
											if(jumpMark==100){  //说明是第一次进入的情况下跳入的
												startActivity(new Intent(LoginActivity.this, MainActivity.class));
												
											}
											
											finish();
										}else{
											Toast.makeText(LoginActivity.this, getString(R.string.loginError), 0).show();
										}
										
									}
									
								};
								
								public void onFinish() {
									
									progressHUD.dismiss();
								};
								
								public void onFailure(Throwable arg0, String arg1) {
									progressHUD.dismiss();
									Toast.makeText(LoginActivity.this, getString(R.string.serverError), 0).show();
								};
								
							});
							
						}else{
							Toast.makeText(LoginActivity.this, getString(R.string.checkUserOrPass), 0).show();
							
						}
						
						
						
						
						
					}else{
						Toast.makeText(LoginActivity.this, getString(R.string.checkUserOrPass), 0).show();
					}
					
				}
			});
		
			findViewById(R.id.jumpLogin).setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					DataStorageHelper settingDataStorageHelper = new DataStorageHelper(LoginActivity.this, SPSetting.SETTING_TABLE_NAME);
					if(settingDataStorageHelper.getBoolean(SPSetting.IS_FIRST_APP_IN)==false){
						settingDataStorageHelper.putBoolean(SPSetting.IS_FIRST_APP_IN, true);
						startActivity(new Intent(LoginActivity.this, FirstInAdressActivity.class));
					}else{
						startActivity(new Intent(LoginActivity.this, MainActivity.class));
					}
					finish();
					
				}
			});
			
			
	}
}
